<template>
  <div class="login-container">
    <el-form ref="form" :rules="rules" class="form-container" :model="formData">
      <p class="title">Node-Vue-Blog</p>
      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="people_fill"></svg-icon>
        </span>
        <el-input v-model="formData.account" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="lock_fill"></svg-icon>
        </span>
        <el-input v-model="formData.password" type="password" placeholder="密码"></el-input>
      </el-form-item>
      <div class="register-forget">
        <p>没有账号？<router-link to="/register">注册</router-link></p>
        <p><router-link to="/forget">忘记密码?</router-link></p>
      </div>
      <el-button class="login-btn" type="primary" @click="login">登录</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validateAccout = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        account: '',
        password: ''
      },
      rules: {
        account: [{ validator: validateAccout, trigger: 'blur' }],
        password: [{ validator: validatePassword, trigger: 'blur' }]
      }
    }
  },
  methods: {
    login() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$store.dispatch('user/login', this.formData).then((res) => {
            if (res.errcode === 0) {
              this.$router.push('/')
            } else {
              this.$message.error(res.errmsg)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less">
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background-color: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #fff;
      height: 47px;
      caret-color: #fff;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #283443 inset !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background-color: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>
<style lang="less" scoped>
.login-container {
  height: 100vh;
  background-color: #283443;
  .form-container {
    width: 500px;
    position: absolute;
    left: 50%;
    top: 50%;
    overflow: hidden;
    transform: translate(-50%, -50%);
    .title {
      color: rgba(255, 255, 255, 0.7);
      text-align: center;
      font-size: 28px;
      font-weight: bold;
    }
    .svg-container {
      padding: 2px 5px 6px 15px;
      font-size: 20px;
      color: #889aa4;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }
    .register-forget {
      display: flex;
      justify-content: space-between;
      flex-wrap: nowrap;
    }
    p {
      font-size: 12px;
      color: #fff;
      margin: 5px 0;
      a {
        color: #3a8ee6;
      }
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
